<?php

class AdminController extends Zend_Controller_Action
{

    public function preDispatch()
    {
        $this->view->render('admin/_navigation.phtml');
    }

    public function init()
    {
        $this->_helper->layout->setLayout('admin');
        $adminSession = new Zend_Session_Namespace('Store_Admin');
        $adminSession->setExpirationSeconds(3600);

    }

    public function indexAction()
    {
        $adminSession = new Zend_Session_Namespace('Store_Admin');

        if ($adminSession->adminId) {
            $this->_helper->redirector('dashboard', 'admin');
            return;
        }
        $form    = new Application_Form_Admin();
        $this->view->form = $form;
    }

    public function loginAction()
    {
        $loginForm = new Application_Form_Admin();

        if ($loginForm->isValid($_POST)) {
            $adminSession = new Zend_Session_Namespace('Store_Admin');
            $adminSession->setExpirationSeconds(3600);
            $emso = $loginForm->getValue('emso');
            $geslo = $loginForm->getValue('geslo');
            
            $adminMapper = new Application_Model_AdminUserMapper();

            $admUser = $adminMapper->authenticate($emso, $geslo);

            if ($admUser != null) {
                $adminSession->adminId = $admUser->getEmso();
                $adminSession->ime = $admUser->getIme()." ".$admUser->getPriimek();
                $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
                $logger = new Zend_Log($writer);

                $logger->info("Admin: ".$adminSession->ime." EMŠO: ".$adminSession->adminId." se je prijavil");

            } else {
                $adminSession->error = "Napacni podatki!";
            }
            $this->_helper->redirector('index', 'admin');
        }
    }

    public function dashboardAction()
    {
        $adminSession = new Zend_Session_Namespace('Store_Admin');
        if (!$adminSession->adminId) {
            $this->_helper->redirector('index', 'admin');
        }
    }

    public function logoutAction()
    {
        $adminSession = new Zend_Session_Namespace('Store_Admin');
        if (!$adminSession->adminId) {
            $this->_helper->redirector('index', 'admin');
        }
        $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
        $logger = new Zend_Log($writer);

        $logger->info("Admin: ".$adminSession->ime." EMŠO: ".$adminSession->adminId." se je odjavil");
        $adminSession->unsetAll();
        $this->_helper->redirector('index', 'admin');
    }

    public function editAction()
    {
        $adminSession = new Zend_Session_Namespace('Store_Admin');

        if (!$adminSession->adminId) {
            $this->_helper->redirector('index', 'admin');
            return;
        }
        $form    = new Application_Form_AdminEdit();
        $this->view->form = $form;
    }

    public function sellerGridAction()
    {
        $adminSession = new Zend_Session_Namespace('Store_Admin');
        if ($adminSession->adminId) {

        } else {
            $adminSession->error = "Ta akcija je na voljo samo administratorju!";
            $this->_helper->redirector('index', 'index');
        }
    }

    public function editPostAction()
    {
        $adminSession = new Zend_Session_Namespace('Store_Admin');
        $editForm = new Application_Form_AdminEdit();
        $emso =  $adminSession->adminId;

        if ($adminSession->adminId) {
            // Polja v $editForm se napolnejo šele po klicu isValid
            if ($editForm->isValid($_POST)) {
                $geslo = $editForm->getValue('geslo');
                $adminMapper = new Application_Model_AdminUserMapper();
                $admUser = $adminMapper->authenticate($emso, $geslo);
                if ($admUser != null) {
                    if ($editForm->getValue('spremeni_geslo')) {
                        if (strlen(trim($editForm)) > 0 && $editForm->getValue('geslo_novo') == $editForm->getValue('geslo_potrditev')) {
                            $admUser->setPriimek($editForm->getValue('priimek'));
                            $admUser->setIme($editForm->getValue('ime'));
                            $admUser->setGeslo(password_hash($editForm->getValue('geslo_novo'), PASSWORD_DEFAULT));
                            $adminMapper->save($admUser);
                            $adminSession->success = "Shranjevanje uspešno";
                            $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
                            $logger = new Zend_Log($writer);

                            $logger->info("Admin: ".$adminSession->ime." EMŠO: ".$adminSession->adminId." je posodobil svoje podatke");
                        } else {
                            $adminSession->error = "Napaka pri nastavljanju novega gesla. Prosim preveri podatke.";
                        }
                    } else {
                        $admUser->setPriimek($editForm->getValue('priimek'));
                        $admUser->setIme($editForm->getValue('ime'));
                        $adminMapper->save($admUser);
                        $adminSession->success = "Shranjevanje uspešno";
                        $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
                        $logger = new Zend_Log($writer);

                        $logger->info("Admin: ".$adminSession->ime." EMŠO: ".$adminSession->adminId." je posodobil svoje podatke");

                    }
                } else {
                    $adminSession->error = "Napačno geslo!";
                }
            }
        } else {
            $adminSession->error = "Ta akcija je na voljo samo administratorju!";
        }
        $this->_helper->redirector('edit', 'admin');

    }

    public function ustvariProdajalcaAction()
    {
        $adminSession = new Zend_Session_Namespace('Store_Admin');

        if ($adminSession->adminId) {
            $form = new Application_Form_NovProdajalec();
            $this->view->form = $form;
        } else {
            $adminSession->error = "Ta akcija je na voljo samo administratorju!";
        }
    }

    public function ustvariProdajalcaPostAction()
    {
        $adminSession = new Zend_Session_Namespace('Store_Admin');

        if ($adminSession->adminId) {

            $editForm = new Application_Form_NovProdajalec();

            // Polja v $editForm se napolnejo šele po klicu isValid
            if ($editForm->isValid($_POST)) {
                $emso = $editForm->getValue('emso');
                $geslo = $editForm->getValue('geslo');
                $sellerMapper = new Application_Model_SellerMapper();

                $seller = new Application_Model_Seller();

                $seller->setEmso($emso);
                $seller->setGeslo(password_hash($geslo, PASSWORD_DEFAULT));
                $seller->setPriimek($editForm->getValue('priimek'));
                $seller->setIme($editForm->getValue('ime'));

                try {
                    $sellerMapper->save($seller);
                    $adminSession->success = "Shranjevanje prodajalca uspešno";
                    $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
                    $logger = new Zend_Log($writer);

                    $logger->info("Admin: ".$adminSession->ime." EMŠO: ".$adminSession->adminId." je ustvaril ali uredil novega prodajalca z EMŠO: ".$seller->getEmso());
                } catch (Exception $e) {
                    $adminSession->error = $e->getMessage();
                }
            } else {
                $adminSession->error = "Neveljaven post!";
            }
        } else {
            $adminSession->error = "Ta akcija je na voljo samo administratorju!";
        }
        $this->_helper->redirector('seller-grid', 'admin');
    }

    public function editProdajalecAction()
    {
        $adminSession = new Zend_Session_Namespace('Store_Admin');

        if ($adminSession->adminId) {
            $form = new Application_Form_NovProdajalec();

            $sellerEmso = base64_decode($this->getParam('id'));

            $seller = new Application_Model_Seller();
            $sellerMapper = new Application_Model_SellerMapper();

            $sellerMapper->find($sellerEmso, $seller);

            $data = array(
                "emso" => $seller->getEmso(),
                "ime" => $seller->getIme(),
                "priimek" => $seller->getPriimek(),
            );

            $form->setDefaults($data);
            $this->view->form = $form;
        } else {
            $adminSession->error = "Ta akcija je na voljo samo administratorju!";
            $this->_helper->redirector('seller-grid', 'admin');

        }
    }

    public function deleteProdajalecAction()
    {
        $form    = new Application_Form_IdForm();
        $form->setDefaults(array('id' => $this->getParam('id')));
        $this->view->form = $form;
    }

    public function aktivirajAction()
    {
        $form    = new Application_Form_IdForm();
        $form->setDefaults(array('id' => $this->getParam('id')));
        $this->view->form = $form;
    }

    public function deaktivirajAction()
    {
        $form    = new Application_Form_IdForm();
        $form->setDefaults(array('id' => $this->getParam('id')));
        $this->view->form = $form;
    }

    public function aktivirajPostAction()
    {
        $adminSession = new Zend_Session_Namespace('Store_Admin');
        $idForm = new Application_Form_IdForm();

        if ($adminSession->adminId && $idForm->isValid($_POST)) {
            $emso = base64_decode($idForm->getValue('id'));
            $sellerMapper = new Application_Model_SellerMapper();
            $sellerMapper->update($emso, "aktiviran", 1);

            $adminSession->success = "Aktivacija prodajalca uspešna";

            $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
            $logger = new Zend_Log($writer);

            $logger->info("Admin: ".$adminSession->ime." EMŠO: ".$adminSession->adminId." je aktiviral prodajalca z EMŠO: ".$emso);
        } else {
            $adminSession->error = "Ta akcija je na voljo samo administratorju!";
        }
        $this->_helper->redirector('seller-grid', 'admin');
    }

    public function dektivirajPostAction()
    {
        $adminSession = new Zend_Session_Namespace('Store_Admin');
        $idForm = new Application_Form_IdForm();

        if ($adminSession->adminId && $idForm->isValid($_POST)) {
            $emso = base64_decode($idForm->getValue('id'));
            $sellerMapper = new Application_Model_SellerMapper();
            $sellerMapper->update($emso, "aktiviran", 0);

            $adminSession->success = "Deaktivacija prodajalca uspešna";
            $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
            $logger = new Zend_Log($writer);

            $logger->info("Admin: ".$adminSession->ime." EMŠO: ".$adminSession->adminId." je deaktiviral prodajalca z EMŠO ".$emso);
        } else {
            $adminSession->error = "Ta akcija je na voljo samo administratorju!";
        }
        $this->_helper->redirector('seller-grid', 'admin');
    }

    public function deleteProdajalecPostAction()
    {
        $adminSession = new Zend_Session_Namespace('Store_Admin');
        $idForm = new Application_Form_IdForm();

        if ($adminSession->adminId && $idForm->isValid($_POST)) {
            $emso = base64_decode($idForm->getValue('id'));
            $sellerMapper = new Application_Model_SellerMapper();
            $sellerMapper->delete($emso);

            $adminSession->success = "Brisanje prodajalca uspešno";
            $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
            $logger = new Zend_Log($writer);

            $logger->info("Admin: ".$adminSession->ime." EMŠO: ".$adminSession->adminId." je izbrisal prodajalca z EMŠO ".$emso);
        } else {
            $adminSession->error = "Ta akcija je na voljo samo administratorju!";
        }
        $this->_helper->redirector('seller-grid', 'admin');
    }


}































